package com.bw.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bw.bean.OrderA;
import com.bw.bean.OrderB;
import com.bw.bean.OrderC;
import com.bw.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController

public class OrderController {
    @Autowired
    OrderService service;

    //命名为listA
    @RequestMapping("listA")
    public JSONObject listA(){
        //获取对应的listA集合
        List<OrderA> orderAS = service.listA();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");

        JSONObject object = new JSONObject();

        JSONArray arr1 = new JSONArray();
        //通过集合遍历进行给arr1中的值赋值
        for (OrderA orderA : orderAS) {
            arr1.add(orderA.getCityName());
        }
        object.put("categories",arr1);

        JSONArray arr2 = new JSONArray();
        JSONObject arr2Obj = new JSONObject();
        arr2Obj.put("name","城市");

        JSONArray arr2ObjArr = new JSONArray();
        //循环赋值将数值给到arr2ObjArr
        for (OrderA orderA : orderAS) {
            arr2ObjArr.add(orderA.getTotal());
        }

        arr2Obj.put("data",arr2ObjArr);
        //将对象添加到arr2中
        arr2.add(arr2Obj);

        object.put("series",arr2);
        jsonObject.put("data",object);
        return jsonObject;
    }
    //命名为listB
    @RequestMapping("listB")
    public JSONObject listB(){
        //获取对应的listB集合
        List<OrderB> orderBS = service.listB();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");

        JSONArray arr = new JSONArray();
        //通过集合遍历给arr赋值
        for (OrderB orderB : orderBS) {
            //循环新建对象
            JSONObject object = new JSONObject();
            object.put("name",orderB.getTypeName());
            object.put("value",orderB.getZb());
            //将新建的对象添加到arr中
            arr.add(object);
        }
        jsonObject.put("data",arr);
        return jsonObject;
    }
    //命名为listC
    @RequestMapping("listC")
    public JSONObject listC(){
        //获取对应的listC对象
        OrderC orderC = service.listC();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        jsonObject.put("data",orderC.getTotal());
        return jsonObject;
    }
}
